import leftopic from '@/components/leftopic'
import loading from '@/components/loading'
import vueTop from '@/components/vueTop'
import foodBBSChild from '@/components/foodBBS_child'
import Addarticle from '@/components/foodBBS_addartcile'
export default {
  name: 'test',
  data () {
    return {
      bbsctx_bool:false,
      addbbsctx_bool:false,
      bbs_child:{
        context:'',
        publictime:'',
        topic:'',
        user:'',
        admirnum:''
      },
      hotarticle:{
        topic:'中国人吃海鲜的历史由来',
        context:'中国的农耕社会特质，决定了海鲜在古人的饮食结构中是奢侈的象征――海错经常都是与山珍并列的。据《周礼?天官》记载，古代的鱼、螺、蛤蜊等，或是加工成祭品供周天子祭祖用，或是供周朝宫廷内食用。也正因为海鲜出入宫廷，得以流传下诸多关于海鲜菜品的记载。',
      },
      items: [],
      selectarticle:{
        recomctx:'',
        artcilid:''
      },
      item2s:[],
      morindex:0,
      file:'',
      article:{
        topic:'1',
        ctx:'1'
      },
      artciesle:''
    }
  },
  components:{
    leftopic,
    loading,
    foodBBSChild,
    vueTop,
    Addarticle
  },
  methods: {
    clearFiles: function() {
      this.$refs.fileinput.reset();
      this.article.topic='';
      this.article.ctx='';
    },
    artcileupload: function() {
      alert("上传");
      var tf_name = this.file.name;
      var tf_topic = this.article.topic;
      var tf_ctx = this.article.ctx;
      var tf_user = this.$store.state.loginmes.name;
      var myDate=new Date();
      var month=myDate.getMonth()+1;
      var tf_time=myDate.getFullYear()+'-'+month+'-'+myDate.getDate()+' '+myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds();
      console.log("获取缓存后的file：", tf_name);
      this.$http({
        method: "post",
        url: "/api/Uploadimage", //显示当前学生的选课信息
        data: {
          sfname: tf_name,
          stopic: tf_topic,
          sctx: tf_ctx,
          suser: tf_user,
          stime: tf_time,
        },
      }).then(
        result => {
          let arr = result.data;
          let self=this;
          if(arr.status=='200'){
            alert('文章发送成功');
          }
          //console.log(self.items);
          console.log(arr);
        },
        err => {
          console.log(err);
        }
      );
    },
    selFile: function(e) {
      var self = this;
      //黑科技获取本地绝对路径难度高，转换为本地缓存
      //console.log(e.target.files[0]);
      var formData = new FormData();
      formData.append("filename", e.target.files[0].name);
      formData.append("upfile", e.target.files[0]);
      formData.append("filetype", e.target.files[0].type);
      formData.append("filesize", e.target.files[0].size);
      this.$http({
        method: "post",
        url: "/api/Uploadtemporary",
        data: formData,
        headers: {
          "Content-Type": "multipart/form-data"
        }
      }).then(
        response => {
          console.log(response);
        },
        err => {
          console.log(err);
        }
      );
    },
    getbbstartmes:function(){
      var _this=this;
      this.$http.post(
        "/api/selectfoodbbs",
        {
          // data
        })
        .then(function (res) {
          //console.log(res.data.data);
          //console.log("请求成功");
          var datale=res.data.data.length;
          _this.artciesle=datale;
          var option={
            topic:'1',
            topic2:'2'
          }
          for(var i=0;i<9;i++){
            _this.items.push(option);
            _this.items[i].topic=res.data.data[i].topic;
            _this.items[i].topic2=res.data.data[i+9].topic;
            option={
              topic:'1',
              topic2:'2'
            }
          }
        })
        .catch(function (err) {
          console.log(err);
          console.log("服务器错误");
        });
    },
    //锚点跳转
    jump:function(index){
      let jump = document.querySelectorAll('#recommend')
      // 获取需要滚动的距离
      let total = jump[index].offsetTop
      // Chrome
      document.body.scrollTop = total
      // Firefox
      document.documentElement.scrollTop = total
      // Safari
      window.pageYOffset = total
    },
    deletreview:function(index){
      //alert('删除'+index);
      //this.item2s[index].usercombool=false;
      var self=this;
      //self.item2s.splice(index,1);
      let option={
        user:self.item2s[index].user,
        ctx:self.item2s[index].ctx,
      }
      this.$http.post(
        "/api/deletbbscomMsg",
        {
          dataMsg:option,
        })
        .then(function (res) {
          if(res.data.status=='200'){
            alert('删除成功');
            self.item2s.splice(index,1);
            console.log(res.data);
          }else {
            alert('删除失败');
          }
        })
        .catch(function (err) {
          console.log(err);
          console.log("服务器错误");
        });
    },
    myreview:function () {
      var self=this;
      if(this.$store.state.loginmes.name==''){
        alert("请先登录");
      }else{
        var myDate=new Date();
        var month=myDate.getMonth()+1;
        var mytime=myDate.getFullYear()+'-'+month+'-'+myDate.getDate()+' '+myDate.getHours()+':'+myDate.getMinutes()+':'+myDate.getSeconds();
        let option={
          user:self.$store.state.loginmes.name,
          articlid:self.selectarticle.artcilid,
          time:mytime,
          ctx:self.selectarticle.recomctx,
          local:self.item2s.length+1,
          usercombool:true
        }
        this.$http.post(
          "/api/addbbscomMsg",
          {
            dataop:option
          })
          .then(function (res) {
            alert('发送成功');
            self.item2s.push(option);
            self.selectarticle.recomctx='';
            //console.log(res.data);
          })
          .catch(function (err) {
            console.log(err);
            console.log("服务器错误");
          });
      }
    },
    gotoctx:function($index){
      var atopic=this.items[$index].topic;
      console.log($index,atopic);
      this.bbsctx_bool=true;
      var _this=this;
      this.$http.post(
        "/api/selectfoodctxMsg",
        {
          articleTopic:atopic
        })
        .then(function (res) {
          //评论信息
          for(var i=0;i<res.data.data.length;i++){
            if(res.data.data[i].recomuser){
              let option={
                user:res.data.data[i].recomuser,
                time:res.data.data[i].recomtime,
                local:i+1,
                ctx:res.data.data[i].recomctx,
                usercombool:false,
              }
              if(_this.$store.state.loginmes.name==res.data.data[i].recomuser){
                option.usercombool=true;
              }
              _this.item2s.push(option);
              //console.log(res.data.data[i].recomuser);
              option={
                user:'',
                time:'',
                local:'',
                ctx:'',
                usercombool:false,
              }
            }
          }
          console.log(res.data.data[0]);
          console.log("请求成功");
          _this.bbs_child.topic=res.data.data[0].topic;
          _this.bbs_child.context=res.data.data[0].context;
          _this.bbs_child.publictime=res.data.data[0].publictime;
          _this.bbs_child.user=res.data.data[0].user;
          _this.bbs_child.admirnum=res.data.data[0].admirnum;
          _this.selectarticle.artcilid=res.data.data[0].articlid;
        })
        .catch(function (err) {
          console.log(err);
          console.log("服务器错误");
        });
    },
    gotoctx2:function($index){
      var atopic=this.items[$index].topic2;
      console.log($index,atopic);
      this.bbsctx_bool=true;
      var _this=this;
      this.$http.post(
        "/api/selectfoodctxMsg",
        {
          articleTopic:atopic
        })
        .then(function (res) {
          for(var i=0;i<res.data.data.length;i++){
            if(res.data.data[i].recomuser){
              let option={
                user:res.data.data[i].recomuser,
                time:res.data.data[i].recomtime,
                local:i+1,
                ctx:res.data.data[i].recomctx,
                usercombool:false
              }
              if(_this.$store.state.loginmes.name==res.data.data[i].recomuser){
                option.usercombool=true;
              }
              _this.item2s.push(option);
              //console.log(res.data.data[i].recomuser);
              option={
                user:'',
                time:'',
                local:'',
                ctx:'',
                usercombool:false
              }
            }
          }
          console.log(res.data.data[0]);
          console.log("请求成功");
          _this.bbs_child.topic=res.data.data[0].topic;
          _this.bbs_child.context=res.data.data[0].context;
          _this.bbs_child.publictime=res.data.data[0].publictime;
          _this.bbs_child.user=res.data.data[0].user;
          _this.bbs_child.admirnum=res.data.data[0].admirnum;
          _this.selectarticle.artcilid=res.data.data[0].artcilid;
        })
        .catch(function (err) {
          console.log(err);
          console.log("服务器错误");
        });
    },
    getwoart:function () {
      var _this=this;
      this.$http.post(
        "/api/selectfoodbbs",
        {
          // data
        })
        .then(function (res) {
          //console.log(res.data.data);
          console.log("请求成功");
          var datale=res.data.data.length
          var option={
            topic:'',
            topic2:''
          }
          var le=_this.items.length+_this.morindex;
          option={
            topic:res.data.data[le].topic,
            topic2:res.data.data[le+1].topic
          }
          _this.items.push(option);
          _this.morindex++;
        })
        .catch(function (err) {
          console.log(err);
          console.log("服务器错误");
        });
    },
    closeartic:function(){
      this.bbsctx_bool=false;
      let le=this.item2s.length;
      this.item2s.splice(0,le);
    },
    openaddarticle:function () {
      this.addbbsctx_bool=true;
    },
    closeaddartic:function () {
      this.addbbsctx_bool=false;
    }
  },
  mounted:function(){
    this.getbbstartmes();
  },
}
